package com.key.controller.admin.workflow;

import com.key.common.R;
import com.key.entity.Approval;
import com.key.entity.MainInfoSingle;
import com.key.service.AdminService;
import com.key.service.ApprovalService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * @Author: li
 * @CreateTime: 2023-02-18  15:14
 */

@Controller
@RequestMapping("/approval")
@Api(value = "我的审批模块",tags = "我的审批模块接口")
//我的审批模块相关功能
public class ApprovalController {

    @Autowired
    ApprovalService approvalService;

    /**
     * 显示所有待审批信息，包括高级搜素
     * @param approval
     * @return
     */
    @RequestMapping(value = "/showAllPendingApprovalInfo",method = RequestMethod.POST)
    @ApiOperation(value = "审批信息接口",notes = "查询所有待审批信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllPendingApprovalInfo(@RequestBody @ApiParam(name = "approval",value = "审批单信息 ",required = false)Approval approval){

        R r = approvalService.showAllPendingApprovalInfo(approval);

        return r;
    }
    /**
     * 显示所有已审批信息，包括高级搜素
     * @param approval
     * @return
     */
    @RequestMapping(value = "/showAllAlreadyApprovalInfo",method = RequestMethod.POST)
    @ApiOperation(value = "审批信息接口",notes = "查询所有已审批信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showAllAlreadyApprovalInfo(@RequestBody @ApiParam(name = "approval",value = "审批单信息 ",required = false)Approval approval){

        R r = approvalService.showAllAlreadyApprovalInfo(approval);

        return r;
    }

    /**
     * 审批通过，修改相关信息
     * @param ids
     * @return
     */
    @RequestMapping(value = "/adopt",method = RequestMethod.POST)
    @ApiOperation(value = "审批信息接口",notes = "审批通过，修改相关信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R adopt(@RequestBody @ApiParam(name = "ids",value = "审批单id ",required = false) Integer[] ids){

        R r = approvalService.approvalYes(ids);

        return r;
    }

    /**
     * 审批驳回，修改相关信息
     * @param ids
     * @return
     */
    @RequestMapping(value = "/reject",method = RequestMethod.POST)
    @ApiOperation(value = "审批信息接口",notes = "审批驳回，修改相关信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R reject(@RequestBody @ApiParam(name = "ids",value = "选中的审批id ",required = false) Integer[] ids){

        R r = approvalService.approvalNo(ids);

        return r;
    }

    /**
     * 查询单个领用/借用单信息
     * @param num
     * @return
     */
    @RequestMapping(value = "/assetsUserInfo",method = RequestMethod.GET)
    @ApiOperation(value = "审批信息接口",notes = "查询单个领用/借用单信息",httpMethod = "get",response = Json.class)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num",value = "选中的单号",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "state",value = "选中的申请类型",paramType = "query",dataType = "String",required = true)

    })

    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R assetsUserInfo(@RequestParam(name = "num") String num,
                            @RequestParam(name = "state") String state){

        R r = approvalService.showApprovalByIdAndState(num, state);

        return r;
    }

    /**
     * 显示员工借用申请信息
     * @param approval
     * @return
     */
    @RequestMapping(value = "/showEmpBorrowInfo",method = RequestMethod.POST)
    @ApiOperation(value = "员工借用申请管理",notes = "显示员工借用申请信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showEmpBorrowInfo(@RequestBody @ApiParam(name = "approval",value = "审批单信息 ",required = false)Approval approval){

        R r = approvalService.showEmpBorrowInfo(approval);

        return r;
    }

    /**
     * 显示员工领用申请信息
     * @param approval
     * @return
     */
    @RequestMapping(value = "/showEmpCollectingInfo",method = RequestMethod.POST)
    @ApiOperation(value = "员工领用申请管理",notes = "显示员工领用申请信息",httpMethod = "post",response = Json.class)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R showEmpCollectingInfo(@RequestBody @ApiParam(name = "approval",value = "审批单信息 ",required = false)Approval approval){

        R r = approvalService.showEmpCollectingInfo(approval);

        return r;
    }

    /**
     * 通过员工申请和领用
     * @param ids
     * @return
     */
    @RequestMapping(value = "/assetsUserInfo",method = RequestMethod.POST)
    @ApiOperation(value = "审批信息接口",notes = "通过员工申请和领用",httpMethod = "Post",response = Json.class)
    @ResponseBody



    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R assetsProvideEmp(@RequestBody @ApiParam(name = "ids",value = "选中的单号 ",required = false) Integer[] ids){


        R r = approvalService.assetsProvideEmp(ids);

        return r;
    }

    /**
     * 员工申请/领用信息导出Excel文件
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/EmpBorrowInfoExport",method = RequestMethod.GET)
    @ApiOperation(value = "员工申请/领用信息导出",notes = "员工申请/领用信息导出",httpMethod = "get",response = Json.class)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = approvalService.getWork();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "员工申请单"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 员工申请/领用信息导出Excel文件
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/EmpCollectingInfoExport",method = RequestMethod.GET)
    @ApiOperation(value = "员工申请/领用信息导出",notes = "员工申请/领用信息导出",httpMethod = "get",response = Json.class)
    public void exportExcel2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = approvalService.getWork2();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "员工领用单"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
